package com.emm.yixun.marketing.service.impl;

import java.util.List;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.PriceSchemeDetailService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.model.PriceSchemeDetail;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.dto.PriceSchemeDetailDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.dto.RoomPriceSchemeDetailDto;
import com.emm.yixun.common.model.enums.RoomStatusEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade;

@Service("priceSchemeDetailServiceFacade")
@WebService(name = "com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade")
public class PriceSchemeDetailServiceFacadeImpl implements PriceSchemeDetailServiceFacade {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PriceSchemeDetailService service;
	
	@Autowired
	private ProjectRoomService projectRoomService;

	@Override
	public Response<PriceSchemeDetailDto> save(PriceSchemeDetailDto dto) {
		Response<PriceSchemeDetailDto> response = new Response<PriceSchemeDetailDto>();
		try {
			if (null == dto) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			PriceSchemeDetailDto mDto = service.create(dto);
			if (null == mDto) {
				response.fail("80000", "保存数据错误");
				return response;
			}
			response.success(mDto);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}", e);
		}
		return response;
	}

	@Override
	public Response<Integer> updateByPrimaryKey(PriceSchemeDetailDto dto, Boolean selective) {
		Response<Integer> response = new Response<Integer>();
		try {
			if (null == selective || null == dto || null == dto.getId()) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("更新"));
				return response;
			}

			if (selective) {
				int count = service.update(dto);
				if (count > 0) {
					response.success(Integer.valueOf(count));
				} else {
					response.fail("90001", "更新数据失败");
				}
			} else {
				response.fail("90000", "暂未实现,修改参数selective=true即可");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("更新数据错误：{}", e);
		}
		return response;
	}

	@Override
	public Response<Integer> deleteByPrimaryKey(Long id) {
		Response<Integer> response = new Response<Integer>();
		try {
			if (null == id) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("删除"));
				return response;
			}
			PriceSchemeDetailDto mDto = new PriceSchemeDetailDto();
			mDto.setId(id);
			int count = service.destroy(mDto);
			if (count > 0) {
				response.success(Integer.valueOf(count));
			} else {
				response.fail("90001", "删除数据失败");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("删除数据错误：{}", e);
		}
		return response;
	}

	@Override
	public Response<PriceSchemeDetail> findById(Long id) {
		Response<PriceSchemeDetail> response = new Response<PriceSchemeDetail>();
		try {
			if (null == id) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			PriceSchemeDetail entity = service.selectById(id);
			if (null == entity) {
				response.fail("90001", "主键查询数据失败");
			} else {
				response.success(entity);
				;
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("主键查询数据错误：{}", e);
		}
		return response;
	}

	@Override
	public ResponseList<PriceSchemeDetail> findByDto(PriceSchemeDetailDto dto) {
		ResponseList<PriceSchemeDetail> response = new ResponseList<PriceSchemeDetail>();
		try {
			if (null == dto) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<PriceSchemeDetail> list = service.selectListByDto(dto);
			if (null == list) {
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}", e);
		}
		return response;
	}

	@Override
	public Page<PriceSchemeDetail> findPageByDto(PriceSchemeDetailDto dto, Integer begin, Integer row) {
		Page<PriceSchemeDetail> response = new Page<PriceSchemeDetail>();
		try {
			if (null == dto || null == begin || null == row) {
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			Page<PriceSchemeDetail> page = service.getPageModel(dto, begin, row);
			if (null == page.getRows()) {
				response.setSuccess(false);
				response.setErrorMsg("分页数据不存在");
				return response;
			}
			response = page;
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}", e);
		}
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade#selectEntityByRoomId(java.lang.Long)
	 */
	@Override
	public Response<PriceSchemeDetail> selectEntityByRoomId(Long roomId) {
		Response<PriceSchemeDetail> response = new Response<PriceSchemeDetail>();
		try {
			if (null == roomId) {
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			PriceSchemeDetail priceSchemeDetail = service.selectEntityByRoomId(roomId);
			if (null == priceSchemeDetail) {
				response.fail("90001", "根据roomId查询当前最新一条价格方案明细记录失败");
			} else {
				response.success(priceSchemeDetail);
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("根据roomId查询当前最新一条价格方案明细记录错误：{}", e);
		}
		return response;
	}

	@Override
	public Response<Boolean> updateRoomStatusByGroupId(Long groupId) {
		Response<Boolean> response = new Response<Boolean>();
		try {
			PriceSchemeDetailDto detailDto = new PriceSchemeDetailDto();
			detailDto.setPriceSchemeId(groupId);
			List<PriceSchemeDetail> priceList = service.selectListByDto(detailDto);
			if(priceList!=null&&priceList.size()>0){
				for (PriceSchemeDetail obj:priceList) {
					if(obj.getRoomId()!=null){
						ProjectRoom room=projectRoomService.selectById(obj.getRoomId());
						
						RoomPriceSchemeDetailDto roomPriceSchemeDetailDto=service.selectRoomSchemeByRoomId(obj.getRoomId());
						//当房间状态为可售且没有可用价格方案时 设置房间状态未不可售
						if(room.getRoomStatus().equals(RoomStatusEnum.VENDIBILITY.getNo()) && null==roomPriceSchemeDetailDto){
							ProjectRoomDto roomEntity= new ProjectRoomDto();
							roomEntity.setId(room.getId());
							roomEntity.setRoomStatus(RoomStatusEnum.SALEL.getNo());
							projectRoomService.update(roomEntity);
						}
					}
				}
			}
			response.setSuccess(true);
			
		} catch (Exception e) {
			logger.error("updateRoomStatusByGroupId error: ", e);
		}
		return response;
	}

}
